﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using JustinAngel.Neurons.Framework;
using JustinAngel.UnitedNationsNews.Framework;
using JustinAngel.UnitedNationsNews.ItemViewMoels;
using JustinAngel.UnitedNationsNews.Services;
using Microsoft.Phone.Shell;
using Ninject;

namespace JustinAngel.UnitedNationsNews.Pages.Photos
{
    public class PhotosViewModel : VViewModelBase, IShareableOnSocialNetworks
    {
        public PhotosViewModel()
        {
            WithRootModel(OnRootModelAvailable);
        }

        private void OnRootModelAvailable(RootModel root)
        {
            Photos = root.Photos;
            OnSelectedPhotoIndexChanged();
        }

        private List<PhotoItemViewModel> _photos;
        public List<PhotoItemViewModel> Photos
        {
            get { return _photos; }
            set
            {
                _photos = value;
                RaisePropertyChanged(() => Photos);
            }
        }

        private string _currentTitle;
        public string CurrentTitle
        {
            get { return _currentTitle; }
            set
            {
                _currentTitle = value;
                RaisePropertyChanged(() => CurrentTitle);
            }
        }

        private string _currentDescription;
        public string CurrentDescription
        {
            get { return _currentDescription; }
            set
            {
                _currentDescription = value;
                RaisePropertyChanged(() => CurrentDescription);
            }
        }

        private int _selectedPhotoIndex = 0;
        public int SelectedPhotoIndex
        {
            get { return _selectedPhotoIndex; }
            set
            {
                _selectedPhotoIndex = value;
                RaisePropertyChanged(() => SelectedPhotoIndex);
                OnSelectedPhotoIndexChanged();
            }
        }

        private void OnSelectedPhotoIndexChanged()
        {
            if (Photos == null) // tombstoning hell
                return;

            CurrentTitle = Photos[SelectedPhotoIndex].Title;
            CurrentDescription = Photos[SelectedPhotoIndex].Description;
        }

        private bool _isDetailsVisible;
        public bool IsDetailsVisible
        {
            get { return _isDetailsVisible; }
            set
            {
                _isDetailsVisible = value;
                RaisePropertyChangedAndUpdateVSM(() => IsDetailsVisible, value);
                MessengerInstance.Send(new PhotosDetailsPaneVisiblityChangedMessage(value));
            }
        }

        public ICommand PhotoPageTap
        {
            get { return new RelayCommand(() => IsDetailsVisible = !IsDetailsVisible); }
        }

        public ICommand PreviousAppBarButtonClick
        {
            get
            {
                return new RelayCommand(() =>
                    SelectedPhotoIndex =
                        SelectedPhotoIndex - 1 < 0 ?
                        Photos.Count - 1 :
                        SelectedPhotoIndex - 1
                );
            }
        }

        public ICommand NextAppBarButtonClick
        {
            get
            {
                return new RelayCommand(() =>
                    SelectedPhotoIndex =
                        SelectedPhotoIndex + 1 >= Photos.Count ?
                        0 :
                        SelectedPhotoIndex + 1
                );
            }
        }

        [Inject]
        public IMediaLibraryService MediaLibraryService { get; set; }

        [Inject]
        public IDialogService DialogService { get; set; }

        public ICommand SavePictureLocally
        {
            get
            {
                return new RelayCommand<FrameworkElement>(img =>
                {
                    MediaLibraryService.SavePictureOf(img, CurrentTitle);
                    DialogService.ShowMessageBox("This picture was saved to your Windows Phone 7 pictures hub.", "Hooray!", MessageBoxButton.OK);
                });
            }
        }

        #region Implementation of IShareableOnSocialNetworks

        public Uri Link
        {
            get { return Photos[SelectedPhotoIndex].Link; }
        }

        public string Title
        {
            get { return Photos[SelectedPhotoIndex].Title; }
        }

        public string Content
        {
            get { return Photos[SelectedPhotoIndex].Description; }
        }

        #endregion


        protected override void CheckOnApplicationActivated()
        {
            base.CheckOnApplicationActivated();
            if (SelectedPhotoIndex == 0 && CanReadStateForProperty("PhotosViewModel.SelectedPhotoIndex"))
            {
                SelectedPhotoIndex = ReadStateForProperty<int>("PhotosViewModel.SelectedPhotoIndex");
            }
        }

        protected override void OnApplicationDeactivated(Neurons.Framework.Messages.ApplicationDeactivatedMessage obj)
        {
            base.OnApplicationDeactivated(obj);

            WriteStateForProperty("PhotosViewModel.SelectedPhotoIndex", SelectedPhotoIndex);
        }

        private bool _isLoadingPicture;
        public bool IsLoadingPicture
        {
            get { return _isLoadingPicture; }
            set
            {
                _isLoadingPicture = value;
                RaisePropertyChangedAndUpdateVSM(() => IsLoadingPicture, value);
            }
        }

        public ICommand ImageLoaded
        {
            get
            {
                return new RelayCommand(() => IsLoadingPicture = false);
            }
        }

        public ICommand ImageLoading
        {
            get
            {
                return new RelayCommand(() => IsLoadingPicture = true);
            }
        }
    }
}
